<?php 

class AttendWebModule extends WebModule 
{ 
    protected $id='attend';
    protected static $defaultModel = 'AttendDataModel';

    protected function initialize() {
        $this->maxPerPage = $this->getOptionalModuleVar('MAX_RESULTS', 10);
    } 

    protected function initializeForPage() { 
        $user = $this->getUser();
        $identity = $user->getAttribute('identity');
        if ($identity <> '教師') {
            $this->unauthorizedAccess();
        }

        switch ($this->page) 
        { 
            case 'index':
                $uid = $user->getEmail();
                $feedData['BASE_URL'] = "[YOUR_SERVER]";
                $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
                if (!isset($feedData['PARSER_CLASS'])) {
                    $feedData['PARSER_CLASS'] = 'JSONDataParser';
                }
                $controller = AttendDataModel::factory($modelClass, $feedData);
                $listArr = array();
                foreach($controller->items() as $feedData) {
                    $list['title'] = $feedData['schname'].' 班級:'.$feedData['clsno'];
                    $list['url'] = $this->buildBreadcrumbURL('list', array('sch' => $feedData['schname'], 'cls' => $feedData['clsno']), true);
                    $listArr[] = $list;
                }
                $this->assign('listArr', $listArr);
                break;

            case 'list':
                $uid = $user->getEmail();
                $cls = $this->getArg(array('cls'), null);
                $sch = $this->getArg(array('sch'), null);
                $feedData['BASE_URL'] = "[YOUR_SERVER]";
                $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
                if (!isset($feedData['PARSER_CLASS'])) {
                    $feedData['PARSER_CLASS'] = 'StudDataParser';
                }
                $controller = AttendDataModel::factory($modelClass, $feedData);
                
                $start = $this->getArg('start', 0);
                $controller->setStart($start);
                $controller->setLimit($this->maxPerPage);

                foreach ($controller->items() as $i => $item) {
                    $list['title'] = '班級:'.$item[1].' 座號:'.$item[2].'姓名:'.$item[3];
                    $list['url'] = $this->buildBreadcrumbURL('class', array('id' => $item[4], 'cls' => $cls, 'sch' => $sch), true);
                    $listArr[] = $list;
                }

                $totalItems = $controller->getTotalItems();
                $previousURL = null;
                $nextURL = null;

                if ($totalItems > $this->maxPerPage) {
                    $args = $this->args;
                    if ($start > 0) {
                        $args['start'] = $start - $this->maxPerPage;
                        $previousURL = $this->buildBreadcrumbURL($this->page, $args, false);
                    }

                    if (($totalItems - $start) > $this->maxPerPage) {
                        $args['start'] = $start + $this->maxPerPage;
                        $nextURL = $this->buildBreadcrumbURL($this->page, $args, false);
                    }
                }

                $this->assign('maxPerPage',     $this->maxPerPage);
                $this->assign('previousURL',    $previousURL);
                $this->assign('nextURL',        $nextURL);
                $this->assign('listArr', $listArr);
                break;

            case 'class':
                $id = $this->getArg(array('id'), null);
                $cls = $this->getArg(array('cls'), null);
                $sch = $this->getArg(array('sch'), null);

                $listArr = array();
                for($i = 1;$i < 8;$i++) {
                    $listArr[] = array('title' => '第'.$i.'節', 'value' => $i);
                }
                $this->assign('sections', $listArr);
                $this->assign('id', $id);
                $this->assign('cls', $cls);
                $this->assign('sch', $sch);
                break;

            case 'call':
                $uid = $user->getEmail();
                $id = $this->getArg(array('id'), 0);
                $sch = $this->getArg(array('sch'), 0);
                $cls = $this->getArg(array('cls'), 0);
                $section = $this->getArg(array('section'), 0);
                $feedData['BASE_URL'] = "[YOUR_SERVER]";
                $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
                
                $controller = AttendDataModel::factory($modelClass, $feedData);
                $result = $controller->items();
                $this->assign('result', $result);
                $this->assign('id', $id);
                $this->assign('section', $section);
                $this->assign('cls', $cls);
                $this->assign('sch', $sch);
                break;
         } 
     } 
 }
